package de.ars.client.items;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.lcdui.Graphics;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;


import de.ars.client.animator.Animator;
import de.ars.client.animator.IAnimationEventListener;
import de.ars.client.animator.Point;
import de.ars.client.datahandling.IPersonListener;
import de.ars.client.datahandling.Person;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.items.animators.AddPersonItemAnimator;
import de.ars.client.items.animators.SmsSenderAnimator;
import de.ars.client.items.elements.ListItem;
import de.ars.client.paintSystems.ClassicLayout;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;
import de.ars.client.tools.Input;

public class SmsSender extends AbstractSlideItem
{
	private static final int COLOR_BORDER = 0x0010475f;
    private static final int COLOR_WINDOW_BACKGROUND = 0x0025afd4;
    
    public static final int ANIMATION_OPEN_ADD_PERSON_WINDOW = 0;
    public static final int ANIMATION_CLOSE_ADD_PERSON_WINDOW = 1;
    
    private static final int STATE_OPEN_ANIMATION = 0;
    private static final int STATE_OPENED = 1;
    private static final int STATE_CLOSE_ANIMATION = 2;
    private static final int STATE_CLOSED = 3;
    private int state;
    
    private int animationType;
    private int selectedItemIndex;

	private int screenWidth;
	private int screenHeight;
	
	private StringPainter stringPainter;
	private ClassicLayout layout;
	private MainScreen parent;
	
	private Vector defaultEntryItems;
	
	private String smsMessage;
	
	public SmsSender(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		super(_width,_height,_parent,_stringPainter);
		this.screenWidth = _width;
		this.screenHeight = _height;
		this.state = STATE_CLOSED;
		this.animationType = -1;
		this.selectedItemIndex = 0;
		
		this.parent = _parent;
		this.layout = new ClassicLayout( this.screenWidth, this.screenHeight );
    	this.stringPainter = _stringPainter;

    	this.animator = new SmsSenderAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getAddPersonClosePos());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getAddPersonCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.defaultEntryItems = new Vector();
        this.smsMessage ="";
     }
	
	public void setMessageText(String _message)
	{
		this.smsMessage = _message;
	}
	
	public boolean isOpen()
    {
    	return this.state != STATE_CLOSED;
    }
	
	private void sendSMS()
	{
		if (this.smsMessage != "")
		{
			String number = this.parent.getSpecificPerson().getInformation("person.tel"+(this.selectedItemIndex+1));
			try 
			{
				String addr = "sms://"+number;
				MessageConnection conn = (MessageConnection) Connector.open(addr);
				TextMessage msg = (TextMessage)conn.newMessage(MessageConnection.TEXT_MESSAGE);
				msg.setPayloadText(this.smsMessage);
				conn.send(msg);
				conn.close();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
	}
	
	public void clearSMSText()
	{
		this.smsMessage = "";
	}
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
		{
			switch(_key)
	    	{
				case -1:
					if(this.selectedItemIndex > 0)
					{
						this.selectedItemIndex--;
					}
					return true;
				case -2:
					if(this.selectedItemIndex < this.defaultEntryItems.size()-1)
					{
						this.selectedItemIndex++;
					}
					return true;
	    		case -5:
	    			//open
	    			this.defaultEntryItems.removeAllElements();
	    			if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel1") != null)
	    	        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel1"), "person.tel1"));
	    	        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel2") != null)
	    	        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel2"), "person.tel2"));
	    	        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel3") != null)
	    	        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel3"), "person.tel3"));
	    			this.animationType = ANIMATION_OPEN_ADD_PERSON_WINDOW;
					this.animator.openItem();
					this.state = STATE_OPEN_ANIMATION;
					return true;
	    		case -6:
	    			this.animationType = ANIMATION_CLOSE_ADD_PERSON_WINDOW;
	    			this.animator.closeItem();
	    			this.state = STATE_CLOSE_ANIMATION;
	    			this.parent.openSMS();
	    			return true;
	    		case -7:
	    			//SMS Senden
	    			this.sendSMS();
	    			this.parent.clearSMSText();
	    			this.animationType = ANIMATION_CLOSE_ADD_PERSON_WINDOW;
	    			this.animator.closeItem();
	    			this.state = STATE_CLOSE_ANIMATION;
	    			this.defaultEntryItems.removeAllElements();
	    			return true;
	    	}
		}
		return false;
	}
	
	public void paint( Graphics g )
	{
		if(this.state != STATE_CLOSED)
		{
			Point pos = this.animator.getPoint( AddPersonItemAnimator.POINT_POS );
	        Point size = this.animator.getPoint( AddPersonItemAnimator.POINT_SIZE );
	        Point textPos = this.animator.getPoint(AddPersonItemAnimator.POINT_TEXT_POS);
	        
	        g.setColor( COLOR_WINDOW_BACKGROUND );
	        g.fillRect( (int)pos.getX(), (int)pos.getY(), (int)size.getX(), (int)size.getY() );
	        g.setColor( COLOR_BORDER );
	        g.drawRect( (int)pos.getX(), (int)pos.getY(), (int)size.getX(), (int)size.getY() );
	        
	        if (this.state != STATE_CLOSED)
	        	this.drawDefaultEntries(g, textPos);
		}
	}
	
	public void drawDefaultEntries(Graphics _g, Point _pos)
	{
		ListItem currentItem = null;
		int x = (int)_pos.getX();
		int y = (int)_pos.getY();
		int i = 0;
		Enumeration e = this.defaultEntryItems.elements();
        while( e.hasMoreElements() )
        {
         	_g.translate( x - _g.getTranslateX(), y - _g.getTranslateY() );
	        currentItem = (ListItem)e.nextElement();
	        currentItem.paint( _g, i == this.selectedItemIndex);
	        y += currentItem.getHeight() + 15;
	        i++;
        }
	}
	
	public void open()
	{
		this.defaultEntryItems.removeAllElements();
		if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel1") != null)
        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel1"), "person.tel1"));
        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel2") != null)
        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel2"), "person.tel2"));
        if (this.parent.getSpecificPerson().getInformation("person.tel1") != "" && this.parent.getSpecificPerson().getInformation("person.tel3") != null)
        	this.defaultEntryItems.addElement(new ListItem(this.stringPainter, this.parent.getSpecificPerson().getInformation("person.tel3"), "person.tel3"));
		this.animationType = ANIMATION_OPEN_ADD_PERSON_WINDOW;
		this.animator.openItem();
		this.state = STATE_OPEN_ANIMATION;
	}
	
	public void process( long elapsed )
    {
        this.animator.process( elapsed );
    }

	public void animationEvent(int event) 
	{
		if( event == IAnimationEventListener.ANIMATION_STOPPED )
        {
            switch( this.animationType )
            {
            	case ANIMATION_OPEN_ADD_PERSON_WINDOW:
            		this.state = STATE_OPENED;
            		this.animator.resetPosition( true );
            		break;
            	case ANIMATION_CLOSE_ADD_PERSON_WINDOW:
            		this.state = STATE_CLOSED;
            		this.selectedItemIndex = 0;
            		this.animator.resetPosition( true );
            		break;
            }            
            this.animationType = -1;
        }
	}
	
	public boolean isAnimationInProgress()
    {
        return this.animator.getAnimationState() == Animator.ANIMATION_IN_PROGRESS;
    }
}